local game = require "club.threecard.hbsl"

local self_record = {}

self_record.state = 
{
    records = {};
}

self_record.mutations = 
{
    on_reveal = function(store, state, args)
        local b = false;
        for _, t in ipairs(args.records) do
            if t[1] == game.kbe.player().id then
                b = true;
                break;
            end
        end
        
        if not b then
            return;
        end

        local sender_id = args.sender_id;
        local sender = game.store.gameplay.find_player_by_id(sender_id);

        if not sender then
            return;
        end

        local players = {};
        local max_amount = -1;

        for _, t in ipairs(args.records) do
            if t[2] > max_amount then
                max_amount = t[2];
            end
        end

        for _, t in ipairs(args.records) do
            local id = t[1];
            local amount = t[2];
            local player = game.store.gameplay.find_player_by_id(id);
            if not player then
                return;
            end
            table.insert(players, {id=id, name=player.name, amount=amount, head=player.head, is_crown = amount == max_amount, is_hit = false});
        end

        local record = 
        {
            sender = {id=sender_id, name=sender.name, head=sender.head, country=sender.country};
            amount = args.amount;
            count = args.size;
            remain_count = args.remain_size;
            remain_amount = args.remain_amount;
            no = args.n;
            players = players;
        };

        table.insert(state.records, record);

        store.emit("记录变更")
    end
}

return self_record